package org.lex.ftp.ui.find.old.speed;

import java.awt.event.ActionEvent;
import java.util.logging.Logger;

import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import org.lex.explorer.file.SizeRenderer;
import org.lex.ftp.ui.explorer.file.FileNameRenderer;
import org.lex.ftp.ui.speed.ServerStatus;
import org.lex.ftp.ui.speed.TestManager;
import org.lex.ftp.ui.speed.TestThread;
import org.lex.swing.table.DateRenderer;
import org.lex.utils.logging.LogFactory;


public class SpeedTestSearchFrame extends FindFrame implements ChangeListener {
	private TestManager testManager = null;
	private SearchResultTableModel resultTableModel = null;

	public SpeedTestSearchFrame(TestManager testManager) {
		super();
		this.testManager = testManager;
		this.testManager.setFilter(this);
		this.testManager.setChangeListener(this);
		this.initializeComponents();
	}

	// e.getSource() �� TestThread
	public void stateChanged(ChangeEvent e) {
		setProgressBound((int) testManager.getMaximum(), (int) testManager.getValue());
		TestThread thread = (TestThread) e.getSource();
		if (ServerStatus.Found == thread.getServerStatus())
			getResultTableModel().addSearchResult(thread.getSearchResult());
	}

	protected void runButtonActionPerormed(ActionEvent e) {
		getRunButton().setEnabled(false);
		getStopButton().setEnabled(true);
		getCloseButton().setEnabled(false);
		// ===================================
		this.updateFilters();
		Logger gl = LogFactory.getLog("FtpFileSearch");
		gl.fine("KeywordFilter:\n" + keywordFilter.toString());
		gl.fine("DateFilter: " + dateFilter);
		gl.fine("SizeFilter: " + sizeFilter);
		gl.fine("MiscFilter: " + miscFilter);
		this.testManager.startTest(false);
	}

	protected void stopButtonActionPerformed(ActionEvent e) {
		getRunButton().setEnabled(true);
		getStopButton().setEnabled(false);
		getCloseButton().setEnabled(true);
		// ===================================
	}

	private void initializeComponents() {
		this.setSize(700, 520);
		// =======================
		this.getStopButton().setEnabled(false);
		this.getGeneralPane().getLocationPane().setVisible(false);
		// config resultTable
		JTable table = this.getResultTable();
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setModel(this.getResultTableModel());
		/*
		 * Name, Server, Path, Size, Modified, Attribute
		 */
		int[] widths = { 185, 125, 120, 80, 130, 75 };
		TableCellRenderer[] renderers = { new FileNameRenderer(), null, null, new SizeRenderer(), new DateRenderer(), null };
		TableColumnModel model = table.getColumnModel();
		for (int i = 0; i < widths.length; i++) {
			TableColumn column = model.getColumn(i);
			column.setPreferredWidth(widths[i]);
			if (null != renderers[i])
				column.setCellRenderer(renderers[i]);
		}
	}

	private SearchResultTableModel getResultTableModel() {
		if (null == resultTableModel) {
			resultTableModel = new SearchResultTableModel();
		}
		return resultTableModel;
	}
}
